<?php

/**
 * File alter.inc
 *
 * includes all hook_SOMETHING_alter() functions
 */

/**
 * implements hook_form_user_login_alter().
 */
function crabii_form_user_login_alter(&$form, &$form_state, $form_id = NULL) {
  $destination = drupal_get_destination();

  $form['#attached']['css'][] = drupal_get_path('theme', 'crabii') . '/css/forms/user-login.css';

  $form['actions']['links'] = array(
    '#markup' => theme('item_list', array(
      'items' => array(
        l(t('Forgot your password?'), 'user/password'),
        l(t('Sign up'), 'user/register'),
      ),
      'attributes' => array('class' => array('list-unstyled')),
    ))
  );

  $form['actions']['submit']['#attributes']['class'][] = 'btn-primary';
  $form['actions']['submit']['#attributes']['class'][] = 'pull-right';

  $form['#validate'][] = '_crabii_user_login_errors';
  $form['#action'] = url(current_path(), array('query' => $destination));
}

/**
 * helper for collecting errors during logging-in
 *
 * @param $form
 * @param $form_state
 */
function _crabii_user_login_errors(&$form, &$form_state) {
  if (strtoupper(@$_POST['op']) == 'LOG IN' && @$_POST['form_id'] == 'user_login') {
    $form['#_crabii_user_login_errors'] = theme('status_messages', array('display' => 'error'));

    if(isset($_SESSION['messages']) && isset($_SESSION['messages']['error'])) {
      unset($_SESSION['messages']['error']);
    }

    drupal_add_js('jQuery(document).ready(function($) { $("#modalLogin").modal("show") });',
      array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
    );
  }
}

/**
 * implements hook_form_user_pass_alter().
 */
function crabii_form_user_pass_alter(&$form, &$form_state, $form_id = NULL) {
  $form['#attached']['css'][] = drupal_get_path('theme', 'crabii') . '/css/forms/user-pass.css';
  $form['actions']['links'] = array(
    '#markup' => theme('item_list', array(
      'items' => array(
        l(t('Cancel & Go back'), 'user/login'),
      ),
      'attributes' => array('class' => array('list-unstyled')),
    ))
  );

  $form['actions']['submit']['#attributes']['class'][] = 'btn-primary';
  $form['actions']['submit']['#attributes']['class'][] = 'pull-right';
}

/**
 * implements hook_form_user_register_form_alter().
 */
function crabii_form_user_register_form_alter(&$form, &$form_state, $form_id = NULL) {
  $form['#attached']['css'][] = drupal_get_path('theme', 'crabii') . '/css/forms/user-register.css';

  $form['actions']['links'] = array(
    '#markup' => theme('item_list', array(
      'items' => array(
        l(t('Cancel & Go back'), ''),
      ),
      'attributes' => array('class' => array('list-unstyled')),
    ))
  );

  $form['actions']['submit']['#attributes']['class'][] = 'btn-primary';
  $form['actions']['submit']['#attributes']['class'][] = 'pull-right';
  $form['actions']['submit']['#value'] = t('Create account');

  // process to change password label
  _process_password($form);
}

/**
 * implements hook_form_nip_node_form_alter().
 */
function crabii_form_nip_node_form_alter(&$form, &$form_state, $form_id = NULL) {
  $form['#attached']['css'][] = drupal_get_path('theme', 'crabii') . '/css/forms/nip-node-form.css';
  $form['#attached']['css'][] = drupal_get_path('theme', 'crabii') . '/placeholder/placeholder.css';
  $form['#attached']['js'][] = drupal_get_path('theme', 'crabii') . '/js/nip-node-form.js';
  $form['#attached']['js'][] = drupal_get_path('theme', 'crabii') . '/placeholder/placeholder.js';
  $form['#after_build'][] = 'crabii_form_nip_node_form_after_build';
  $form['#validate'][] = '_crabii_nip_node_form_errors';
}

function _crabii_nip_node_form_errors(&$form, &$form_state) {
  if (strtoupper(@$_POST['op']) == 'POST LINK' && @$_POST['form_id'] == 'nip_node_form') {
    $form['#_crabii_nip_node_form_errors'] = theme('status_messages', array('display' => 'error'));

    if(isset($_SESSION['messages']) && isset($_SESSION['messages']['error'])) {
      unset($_SESSION['messages']['error']);
    }

    drupal_add_js('jQuery(document).ready(function($) { $("#modalPostLink").modal("show") });',
      array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
    );
  }
}

/**
 * Nip node form after build
 * altering nip node form to fit in the design
 *
 * @param $form
 * @param $form_state
 * @return mixed
 */
function crabii_form_nip_node_form_after_build($form, &$form_state) {
  LibraryHelper::load(array(
    array('name' => 'jquery.autogrow-textarea'),
  ), $form['#attached']);

  // field link
  $form['field_link'][LANGUAGE_NONE][0]['value']['#title_display'] = 'invisible';
  $form['field_link'][LANGUAGE_NONE][0]['value']['#attributes']['data-placeholder'] = CommonHelper::crabii_icon('paperclip') . '&nbsp;&nbsp;Link URL';

  // field board
  $form['field_board'][LANGUAGE_NONE]['#title_display'] = 'invisible';
  $form['field_board'][LANGUAGE_NONE]['#attributes']['data-placeholder'] = CommonHelper::crabii_icon('tags') . '&nbsp;&nbsp;Select your board';
  $form['field_board'][LANGUAGE_NONE]['#attributes']['data-placeholder-empty'] = '_none';

  // field body
  $form['body'][LANGUAGE_NONE][0]['value']['#title_display'] = 'invisible';
  $form['body'][LANGUAGE_NONE][0]['value']['#resizable'] = FALSE;
  $form['body'][LANGUAGE_NONE][0]['value']['#attributes']['data-placeholder'] = CommonHelper::crabii_icon('comment-o') . '&nbsp;&nbsp;Write your thoughts';

  // buttons
  $form['actions']['#attributes']['class'][] = 'clearfix';
  $form['actions']['submit']['#value'] = 'Post Link';
  $form['actions']['submit']['#attributes']['class'][] = 'btn-submit-style-1';
  $form['actions']['submit']['#attributes']['class'][] = 'pull-right';

  return $form;
}

/**
 * helper to replace vertical_tabs to fieldset
 * since vertical_tabs is not working
 *
 * @param $item
 * @param $key
 */
function _replace_array_values_vertical_tabs_to_fieldsets(&$item, $key) {

  // Search for 'vertical_tabs' values with key '#type' and replace to 'fieldset'
  if ($item == 'vertical_tabs' && $key == '#type') {
    $item = 'fieldset';
  }
}

/**
 * implements hook_form_alter().
 */
function crabii_form_alter(&$form, &$form_state, $form_id) {
  // Go through hole array using 'replace_array_values_vertical_tabs_to_fieldsets' function
  array_walk_recursive($form, '_replace_array_values_vertical_tabs_to_fieldsets');
}

/**
 * implements hook_form_confirm_alter().
 *
 * force to use page--simple template
 */
function crabii_form_node_delete_confirm_alter(&$form, &$form_state, $form_id) {
  _transform_simple_page();

  $form['#attached']['css'][] = drupal_get_path('theme', 'crabii') . '/css/forms/node-delete-confirm.css';
  $form['actions']['submit']['#attributes']['class'][] = 'pull-right';
  $form['actions']['submit']['#attributes']['class'][] = 'btn-primary';
}

/**
 * implements hook_form_user_profile_form_alter().
 */
function crabii_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
  _transform_simple_page();

  $form['#attached']['css'][] = drupal_get_path('theme', 'crabii') . '/css/forms/user-profile-form.css';
  $form['actions']['submit']['#attributes']['class'][] = 'pull-right';
  $form['actions']['change-password'] = array(
    '#markup' => l('Change password', "user/{$form['#user']->uid}/change-password"),
  );
}

/**
 * implements hook_form_change_pwd_page_form_alter().
 */
function crabii_form_change_pwd_page_form_alter(&$form, &$form_state, $form_id) {
  _transform_simple_page();

  $form['#attached']['css'][] = drupal_get_path('theme', 'crabii') . '/css/forms/change-pwd-page-form.css';
  // unset current_pass
  unset($form['current_pass']);
  foreach($form['#validate'] as $i => $validate) {
    if($validate == 'change_pwd_page_validate_current_pass') {
      unset($form['#validate'][$i]);
    }
  }
  unset($form['account']['pass']['#title']);
  $form['submit']['#value'] = t('Change password');
  $form['submit']['#attributes']['class'][] = 'btn-primary';
  $form['submit']['#attributes']['class'][] = 'pull-right';

  // process to change password label
  _process_password($form);
}
